package common.logging;

import java.io.*;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class WebPortalLogger {

   static Logger logger = Logger.getLogger("");
   static String propertiesFileName = "log4jconfig.properties";

   // Varaibles for Log4J messages
   static java.text.SimpleDateFormat df = (java.text.SimpleDateFormat)java.text.DateFormat.getDateInstance();
   static String COLUMN_DELIMITER="|";
   static String START_TIME  ="Start Time  ";
   static String FINISH_TIME ="Finish Time ";
   static String ELAPSED_TIME="Elapsed Time";
   static String UOM_TIME=" ms";

   static {
      try {
         String log4JPropertyFile = "";
         Properties props = new Properties();
         InputStream in = WebPortalLogger.class.getResourceAsStream("/" +
            propertiesFileName);
         props.load(in);
         PropertyConfigurator.configure(props);
      } catch (FileNotFoundException fnfe) {
         System.out.println("WebPortalLogger.java - FileNotFoundException : " + fnfe.toString());
      } catch (IOException ioe) {
         System.out.println("WebPortalLogger.java - IOException : " + ioe.toString());
      } catch (Exception e) {
         System.out.println("WebPortalLogger.java - Exception : " + e.toString());
         e.printStackTrace();
      }
   }

   public static void debug (Object msg) {
      logger.debug(msg);
   }

   public static void info (Object msg) {
      logger.info(msg);
   }

   public static void warn (Object msg) {
      logger.warn(msg);
   }

   public static void error (Object msg) {
      logger.error(msg);
   }

   public static void fatal (Object msg) {
      logger.fatal(msg);
   }

   public static void printStackTrace(Throwable e) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      String beginStackTrace = "   ==== Begin Stack Trace for the Exception  ==== \n";
      String endStackTrace   = "\n =============  End of Stack Trace  =========== \n";
      String stackTrace = "";

      try {
         e.printStackTrace(pw);
         stackTrace = beginStackTrace + sw.toString() + endStackTrace;
         debug(stackTrace);
         sw.close();
         pw.close();
      } catch (Exception exc) {
         debug(exc.toString());
      }
   }

   public static void logStartTime(String tomcatInstance, String appName,
                               String objectName, long t1) {
     df.applyPattern("MM/dd/yyyy hh:mm:ss:S a");
     // log start time
     info(tomcatInstance + COLUMN_DELIMITER + appName + COLUMN_DELIMITER +
          objectName + COLUMN_DELIMITER + START_TIME + COLUMN_DELIMITER +
          df.format(new java.util.Date(t1)));
   }

   public static void logFinishTime(String tomcatInstance, String appName,
                               String objectName, long t1, long t2) {
      df.applyPattern("MM/dd/yyyy hh:mm:ss:S a");
      // log finish time
      info(tomcatInstance + COLUMN_DELIMITER + appName + COLUMN_DELIMITER +
           objectName + COLUMN_DELIMITER + FINISH_TIME + COLUMN_DELIMITER +
           df.format(new java.util.Date(t2)));
      // log elapsed time
      info(tomcatInstance + COLUMN_DELIMITER + appName + COLUMN_DELIMITER +
           objectName + COLUMN_DELIMITER + ELAPSED_TIME + COLUMN_DELIMITER +
           (t2 - t1) + UOM_TIME);
   }

   public static void main(String args[]) {
//      DOMConfigurator.configure("log4jconfig.xml");

      logger.debug("Here is some DEBUG");
      logger.info("Here is some INFO");
      logger.warn("Here is some WARN");
      logger.error("Here is some ERROR");
      logger.fatal("Here is some FATAL");
   }

}
// End of WebPortalLogger.java
